import scrapy


class QingtingSpider(scrapy.Spider):
    name = 'qingting'
    allowed_domains = ['qingting.fm']
    start_urls = ['https://m.qingting.fm/rank']

    def parse(self, response):
        # print('----->', response)
        # 提取30个需要的a标签对象
        a_list = response.xpath("//div[@class='rank-list']//a")
        print(type(a_list))  # <class 'scrapy.selector.unified.SelectorList'>

        # 遍历取到的数据，对每个单独提取需要的数据
        for a_temp in a_list:
            rank_number = a_temp.xpath("./div[@class='badge']//text()")  # 排名
            img_src = a_temp.xpath("./img/@src")  # 图片地址
            title = a_temp.xpath("./div[@class='content']/div[@class='title']/text()")  # 标题
            desc = a_temp.xpath("./div[@class='content']/div[@class='desc']/text()")  # 描述
            play_number = a_temp.xpath(".//div[@class='info-item'][1]/span/text()")  # 播放量
            print("--**-->", rank_number.extract_first(), img_src.extract_first(), title.extract_first(),
                  desc.extract_first(), play_number.extract_first())
            yield {
                "rank_number": rank_number.extract_first(),
                "img_src": img_src.extract_first(),
                "title": title.extract_first(),
                "desc": desc.extract_first(),
                "play_number": play_number.extract_first()
            }
